﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Abp.Dependency;
using Abp.Domain.Repositories;
using Abp.Logging;

namespace Abp0713Demo.HistoryData
{
    public class HistoryManager:ITransientDependency
    {
        readonly IRepository<PointInfo> _pointRepository;
        readonly IRepository<HistoryDataTable,long> _historyDataRepository;

        public HistoryManager(IRepository<PointInfo> pointRepository, IRepository<HistoryDataTable,long> historyDataRepository)
        {
            _pointRepository = pointRepository;
            _historyDataRepository = historyDataRepository;
        }

        public async Task InsertTestData()
        {
            var temperaturePoint = new PointInfo
            {
                Id = 1,
                Name = "温度",
                Unit = "℃"
            };
            var pressurePoint = new PointInfo
            {
                Id = 2,
                Name = "压力",
                Unit = "Bar"
            };
            //先插入数据点
            var pointCount =await _pointRepository.CountAsync();
            if (pointCount < 1) //没有点信息
            {
                
                var task1 = _pointRepository.InsertAsync(temperaturePoint);
                var task2 = _pointRepository.InsertAsync(pressurePoint);

                Task.WaitAll(task1, task2);
                
                
            }
            var hisCount =await _historyDataRepository.LongCountAsync();
            if (hisCount > 0  ) return;


            var endTime = DateTime.Today;
            var beginTime = endTime.AddMonths(-1);

            //模拟温度历史数据
            var temperatureHistory = new List<HistoryDataTable>();
            var tempTime = beginTime.ConvertDataTimeToLong();
            var endFlag = endTime.ConvertDataTimeToLong();
            while (tempTime < endFlag)
            {
                temperatureHistory.Add(new HistoryDataTable
                {
                    Id = tempTime,
                    PointId = temperaturePoint.Id,
                    Value = RandomHelper.Double(10, 100)  // 温度的值  10℃ -- 100 ℃
                });

                var rndTime = RandomHelper.Int(1000 * 60 * 1, 1000 * 60 * 10); // 1分钟 至 10分钟的毫秒数
                tempTime += rndTime;
            }
            //模拟压力历史数据
            var pressureHistory = new List<HistoryDataTable>();
            tempTime = beginTime.ConvertDataTimeToLong();
            endFlag = endTime.ConvertDataTimeToLong();
            while (tempTime < endFlag)
            {
                pressureHistory.Add(new HistoryDataTable
                {
                    Id = tempTime,
                    PointId = pressurePoint.Id,
                    Value = RandomHelper.Double(0.001, 6)  // 压力的值  0.001 Bar-- 6 ℃
                });

                var rndTime = RandomHelper.Int(1000 * 60 * 1, 1000 * 60 * 10); // 1分钟 至 10分钟的毫秒数
                tempTime += rndTime;
            }

            // TODO : 可以用SqlBlock的方式批量插入,此处暂时不用了.
            List<Task> tasks = new List<Task>(50);
            foreach (var hisItem in temperatureHistory)
            {
                //如果异步任务大于50个时,等待执行完后再继续异步
                if (tasks.Count > 50)
                {
                    Task.WaitAll(tasks.ToArray());
                    tasks.Clear();
                }
                var task = _historyDataRepository.InsertAsync(hisItem);
                tasks.Add(task);
            }
            List<Task> tasks2 = new List<Task>(50);
            foreach (var hisItem in pressureHistory)
            {
                //如果异步任务大于50个时,等待执行完后再继续异步
                if (tasks2.Count > 50)
                {
                    Task.WaitAll(tasks2.ToArray());
                    tasks2.Clear();
                }
                var task = _historyDataRepository.InsertAsync(hisItem);
                tasks2.Add(task);
            }
        }


    }
}